<?php
class product extends Zend_Db_Table{
		protected $_name='plm_order';
		protected $_primary='o_id';
		var $dir_url;
		var $web_url;
		var $site_id;

		public function __construct($site){
			parent::__construct();
			$config=Zend_Registry::get('config');
			$this->cache=Zend_registry::get('cache');
			$this->db=Zend_Registry::get('dbAdapter');
			$this->dir_url=$config->$site->dir_url;
			$this->web_url=$config->$site->web_url;
			$this->site_id=$site;
			set_include_path($this->dir_url);
			require 'app/Mage.php';
			Mage::app();
			}
		
		//µÃµ½ËùÓÐ²úÆ·,¸üÐÂÓÚ2010-6-2
		public function getAll($start=0,$limit='all',$type=''){
			if(!$result = $this->cache->load('productData_'.$this->site_id)) {
			$data=array();
			$product = Mage::getModel('catalog/product');
			$collection  = Mage::getModel('catalog/product')->getCollection();
			if(is_numeric($type)){				
				$category = Mage::getModel('catalog/category')->load($type);			
				$collection->addCategoryFilter($category);
				}
			$collection->addAttributeToSelect('id')
						->addAttributeToSelect('name')
						->addAttributeToSelect('url_path')						
						->addAttributeToSelect('price')
						->addAttributeToSelect('image')
						->addAttributeToSelect('weight')
						->addAttributeToSelect('meta_title')
						->addAttributeToSelect('short_description')
						->addAttributeToSelect('sku')
						->addAttributeToSelect('weight')
						->addAttributeToSelect('url_key');
			$collection->getSelect()->order("entity_id desc");
			if ($collection->getItems()) {
				foreach ($collection->getItems() as $_item) {
				$data[] = $_item->toArray();
			}
			$total=sizeof($data);		
			
			if($limit!='all'){
				$data=array_slice($data,$start,$limit);
			}
			$this->cache->save($data,'productData_'.$this->site_id);

			return $data;
			}
			}
			else{
				return $result;
				}
		}
		
		////µÃµ½ËùÓÐ²úÆ·
//		public function getAll($start=0,$limit='all',$type=''){
//			if(!$result = $this->cache->load('myresult_'.$this->site_id)) {
//			$data=array();
//			$product = Mage::getModel('catalog/product');
//			$collection  = Mage::getModel('catalog/product')->getCollection();
//			if(is_numeric($type)){				
//				$category = Mage::getModel('catalog/category')->load($type);			
//				$collection->addCategoryFilter($category);
//				}
//			$collection->addAttributeToSelect('*');
//			$collection->getSelect()->order("entity_id desc");
//			if ($collection->getItems()) {
//				foreach ($collection->getItems() as $_item) {
//				$data[] = $_item->toArray();
//			}
//			$total=sizeof($data);		
//			
//			if($limit!='all'){
//				$data=array_slice($data,$start,$limit);
//			}
//			$this->cache->save($data,'myresult_'.$this->site_id);
//
//			return $data;
//			}
//			}
//			else{
//				return $result;
//				}
//		}
		
		//µÃµ½ÌØ¶¨idµÄ²úÆ·
		public function getSomeProduct($ids,$start="0",$limit="30"){
			$data=array();
			$product = Mage::getModel('catalog/product');			
			$collection  = Mage::getModel('catalog/product')->getCollection();
			$collection->addAttributeToSelect('id')
						->addAttributeToSelect('name')
						->addAttributeToSelect('url_path')						
						->addAttributeToSelect('price')
						->addAttributeToSelect('image')
						->addAttributeToSelect('small_image')
						->addAttributeToSelect('short_description')
						->addAttributeToSelect('sku')
						->addAttributeToSelect('weight')
						->addAttributeToFilter('entity_id',$ids);
			//$collection->getSelect()->limit($start,$limit);
			$collection->getSelect()->order("entity_id desc");
			if ($collection->getItems()) {
				foreach ($collection->getItems() as $_item) {
				$data[] = $_item->toArray();
			}
			$total=sizeof($data);
			$data=array_slice($data,$start,$limit);
			return array("total"=>$total,"data"=>$data);
			} 
		}
		
		public function getAllOrder($start,$limit){
		$orders=array();
		$order = Mage::getModel('sales/order');
		$collection= $order->getCollection()
						   ->addAttributeToSelect('*');
		$data=array();
		if ($collection->getItems()) {
			foreach ($collection->getItems() as $_item) {
				$data[] = $_item->toArray();
			}
		}
		$total=sizeof($data);
		$sum=$this->countAll();
		$site_id=$this->site_id;
		if($total!=$sum){				
			$site_url=$this->web_url;
			$data=array_slice($data,$start,$total-$sum);
			foreach($data as $item){
			  $id=$item['entity_id'];
			  $w = Mage::getSingleton("core/resource")->getConnection("core_write"); 
			  $result=$w->query("select name from sales_flat_order_item where order_id='$id'");
			  while($rows=$result->fetch(PDO::FETCH_ASSOC)){
				  $product_info=$rows['name'];
			  	}
			  $order = Mage::getModel('sales/order')->load($id);
			  $_temp=$order->getPayment()->getOrder()->getShippingAddress()->getData();
			  $address=array("address"=>$_temp['street'].','.$_temp['city']);
			  $name=array("name"=>$_temp['firstname'].','.$_temp['lastname']);
			  $status=array("status"=>$item['status']);
			  $increment_id=array("increment_id"=>$item['increment_id']);
			  $product=array("product_info"=>$product_info);
			  $info=array_merge($_temp,$address,$name,$product,$status,$increment_id);		  
			  $order_info=array(
						'o_site_id'			=> $site_id,
						'o_site_url'		=> $site_url,
						'o_increment_id'	=> $item['increment_id'],						
						'o_increment_date'	=> $item['updated_at'],
						'o_increment_status'=> $item['status'],
						"o_increment_product"=>$product_info,
						'o_client_name'		=> $_temp['firstname'].','.$_temp['lastname'],
						'o_client_country'	=> $_temp['country_id'],
						'o_client_address'	=> $_temp['street'].','.$_temp['city'],
						'o_client_telephone'=> $_temp['telephone']
						);
			$this->insert($order_info);
			}
		}
			$select=$this->db->select();
			$start=($start==0)?1:$start;
			$select->from($this->_name,'*')
					->where('o_site_id= ?',"$site_id")
					->order('o_increment_date desc')					
					->limit($limit,$start);
		$orders=$this->db->fetchAll($select->__toString());	
		$json=json_encode($orders);
		return '({"total":"'.$total.'","results":'.$json.'})';
		}
		
		public function countAll(){
		$select=$this->db->select();
		$select->from($this->_name,'o_id')
			   ->where('o_site_id= ?',$this->site_id);
		$count=count($this->db->fetchAll($select));
		if($count){
			return $count;
			}
		else{
			return 0;
			}
		}
		
		public function toString(){
			return $this->dir_url;
			}
			
		
}
?>